//
//  ExpressViewViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/10/13.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "ExpressStepCell.h"
#import "ExpressInfoHeaderView.h"

#import "ExpressViewViewController.h"

@interface ExpressViewViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) ExpressDetailModel *model;

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) ExpressInfoHeaderView *headView;

@end

@implementation ExpressViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_ExpressInfo];

}

- (void)initView {
    
    self.title = @"物流信息";
    self.view.backgroundColor = UIColorHex(0xffffff);
    
    [self.view addSubview:self.headView];
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.headView.mas_bottom);
    }];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.model.list.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.model.list[indexPath.row].itemHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ExpressStepCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExpressStepCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.model.list[indexPath.row]];
    
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = UIColorHex(0xffffff);

        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"ExpressStepCell" bundle:nil] forCellReuseIdentifier:@"ExpressStepCellID"];
    }
    return _tableview;
}

- (ExpressInfoHeaderView *)headView {
    if (!_headView) {
        _headView = [[NSBundle mainBundle] loadNibNamed:@"ExpressInfoHeaderView"owner:nil options:nil].firstObject;
    }
    return _headView;
}

- (ExpressDetailModel *)model {
    if (!_model) {
        _model = [[ExpressDetailModel alloc] init];
    }
    return _model;
}

- (void)request_ExpressInfo {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.uodId forKey:@"uodId"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_OrderExpressInfo];
    WEAKSELF
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
//        [[TTNoResultManager shareManager] hideNoResultWithView:weakSelf.tableview];

        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {
            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                
                NSDictionary *dataDic = [data valueForKey:@"data"];

                NSDictionary *resultDic = [dataDic valueForKey:@"result"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *list = [resultDic valueForKey:@"list"];
                
                weakSelf.model = [ExpressDetailModel modelWithDictionary:resultDic];
                weakSelf.model.expressLogo = [resultDic valueForKey:@"logo"];
                weakSelf.model.expressNo = [resultDic valueForKey:@"number"];
                weakSelf.model.expressName = [resultDic valueForKey:@"typename"];

                weakSelf.model.list = @[].mutableCopy;
                
                if ([list isKindOfClass:[NSArray class]]) {
                    
                    for (int i = 0; i < list.count; i++) {
                        
                        NSDictionary *listDic = list[i];
                        ExpressStepModel *model = [ExpressStepModel modelWithDictionary:listDic];
                        
                        model.itemHeight = [Utilities getStringHeightWithText:model.status font:[UIFont systemFontOfSize:14] viewWidth:TTScreenWidth-46] + 10 + 35;
                        
                        [weakSelf.model.list addObject:model];
                    }
                }
                
                weakSelf.headView.expressNameLab.text = weakSelf.model.expressName;
                weakSelf.headView.expressIdLab.text = [NSString stringWithFormat:@"%@%@",@"运单号：",weakSelf.model.expressNo];
                [weakSelf.headView.expressLogo sd_setImageWithURL:[NSURL URLWithString:weakSelf.model.expressLogo] placeholderImage:[UIImage imageNamed:@"placeholder"]];
                [weakSelf.tableview reloadData];
            }
        } else {

        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
}

@end
